﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using iLangl.Model;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class ContactDAL : BaseStorageDAL, IContactDAL
	{
		#region IContactDAL Members

		public void CreateContact(Contact contact,IDbTransaction transaction)
		{
			dtoXmlStorage.ContactRow contactRow = Storage.Contact.FindById(contact.Id);

			bool isNew = (contactRow == null);

			if (isNew)
			{
				contactRow = Storage.Contact.NewContactRow();
			}
			contactRow.Id = contact.Id;

			if (contact.Company != null) 
				contactRow.CompanyId = contact.Company.Id;

			if (!String.IsNullOrEmpty(contact.Email))
			contactRow.EMail = contact.Email;
			if (!String.IsNullOrEmpty(contact.FirstName))
				contactRow.FirstName = contact.FirstName;
			if (!String.IsNullOrEmpty(contact.LastName))
				contactRow.LastName = contact.LastName;
			if (!String.IsNullOrEmpty(contact.Phone))
				contactRow.Phone = contact.Phone;
			if (!String.IsNullOrEmpty(contact.Title))
				contactRow.Title = contact.Title;
			if (!String.IsNullOrEmpty(contact.CustomerIdentity))
				contactRow.CustomerIdentity = contact.CustomerIdentity;
			if (!String.IsNullOrEmpty(contact.TPIdentity))
				contactRow.TPIdentity = contact.TPIdentity;


			if(isNew)
			{
				Storage.Contact.AddContactRow(contactRow);
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public void UpdateContact(Contact contact, IDbTransaction transaction)
		{
			CreateContact(contact, transaction);
		}


		public Contact ReadContact(string contactId)
		{
			dtoXmlStorage.ContactRow contactRow = Storage.Contact.FindById(contactId);

			if (contactRow == null)
				return null;

			Contact contact = new Contact();

			contact.Id = contactRow.Id;

			if(!contactRow.IsEMailNull())
				contact.Email = contactRow.EMail;

			if(!contactRow.IsFirstNameNull())
				contact.FirstName = contactRow.FirstName;


			if(!contactRow.IsLastNameNull())
				contact.LastName = contactRow.LastName;

			if (!contactRow.IsPhoneNull())
				contact.Phone = contactRow.Phone;

			if (!contactRow.IsTitleNull())
				contact.Title = contactRow.Title;


			if (!contactRow.IsCustomerIdentityNull())
				contact.CustomerIdentity = contactRow.CustomerIdentity;

			if (!contactRow.IsTPIdentityNull())
				contact.TPIdentity = contactRow.TPIdentity;

			contact.Company = DALFacade.Instance.CompanyDAL.ReadCompany(contactRow.CompanyId);

			return contact;

		}


		public Contact ReadContactByUserAccountId(string userAccountId)
		{
			dtoXmlStorage.UserAccountsRow uar =  Storage.UserAccounts.FindById(userAccountId);

			if (uar == null)
				return null;

			if (uar.IsContactIdNull())
				return null;

			return ReadContact(uar.ContactId);
		}



		public void AssignContactToUserAccount(string userAccountId, string contactId)
		{
			dtoXmlStorage.UserAccountsRow uar = Storage.UserAccounts.FindById(userAccountId);

			if (uar != null)
			{
				uar.ContactId = contactId;
				AcceptChanges();
			}
		}



		public IList<Contact> GetContacts(string companyId)
		{
			IList<Contact> result = new List<Contact>();

			foreach (dtoXmlStorage.ContactRow cr in Storage.Contact)
			{
				if (string.Compare(cr.CompanyId, companyId, true) == 0)
				{
					result.Add(ReadContact(cr.Id));
				}
			}

			return result;
		}


		#endregion
	}
}
